<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<!-- Casting SPELs in Lisp - Emacs Lisp Edition, a Comic Book
     Written by Conrad Barski, M.D., http://lisperati.com
     Edited by James A. Webb, http://uberkode.com -->

<head>

  <title>Casting SPELs in Lisp (19)</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="author" content="James A. Webb" />
  <meta name="date" content="2007-09-15T00:00:00+00:00" />
  <meta name="keywords" content="Emacs, Lisp" />

</head>

<body>

<center>

<table cellspacing="10" width="500">
  <tr>
    <td>
      Now let's use this function to describe the floor:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (defun describe-floor (loc objs obj-loc)
    (apply #'append (mapcar (lambda (x)
                              `(you see a ,x on the floor -))
                            (remove-if-not (lambda (x)
                                             (is-at x loc obj-loc))
                                           objs))))
</pre>
    </td>
  </tr>
  <tr>
    <td>
      This function has a couple of new things: First of all, it has 
      anonymous functions (<tt>lambda</tt> is just a fancy word for 
      this). That first lambda form is just the same as defining a 
      helper function 
      <tt>(defun blabla (x) `(you see a ,x on the floor.))</tt> 
      and then sending <tt>#'blabla</tt> to the <tt>mapcar</tt> 
      function. The <tt>remove-if-not</tt> function removes any 
      objects from the list that are not at the current location 
      before passing the list on to <tt>mapcar</tt> to build 
      pretty sentences.  Let's try this new function:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (describe-floor 'living-room objects object-locations)
</pre>
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkgreen">
  ==> (you see a whiskey-bottle on the floor - you see a bucket 
       on the floor -)
</pre>
    </td>
  </tr>
  <tr>
    <td>
      Now we can tie all these descriptor functions into a single, 
      easy command called <tt>look</tt> that uses the global 
      variables (therefore this function is not in the 
      <i>Functional Programming</i> style) to feed all the 
      descriptor functions and describes <b>everything</b>:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (defun look ()
    (append (describe-location location map)
            (describe-paths location map)
            (describe-floor location objects object-locations)))
</pre>
    </td>
  </tr>
  <tr>
    <td align="center">
      &lt;&lt; <a href="casting-spels-emacs-1.html">begin</a>
      &lt; <a href="casting-spels-emacs-18.html">previous</a> - 
      <a href="casting-spels-emacs-20.html">next</a> &gt;
      <a href="casting-spels-emacs-35.html" >end</a> &gt;&gt;
    </td>
  </tr>
</table>

</center>

</body>

</html>